package lance.honeydew.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import lance.honeydew.bean.ItemInformation;
import lance.honeydew.bean.OrderDetail;
import lance.honeydew.bean.OrderInformation;

public class OrderInformationDAO extends RootDAO {
	
	private static final Logger logger = Logger.getLogger(OrderInformationDAO.class);
	
	/**
	 * queryOrderInformationByQb
	 * @param qb
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<OrderInformation> queryOrderInformationByQb(OrderInformation qb, int pageNum, int pageSize) {
		
		List<OrderInformation> result = null;
		
		try {
			
			String sqlString = " select o from OrderInformation o " +
					" inner join fetch o.Userid u  " +
					" inner join fetch o.Paymentmethodseqid p  " + 
					" inner join fetch o.Transportmethodseqid t  " + 
					" where 1=1 ";
			
			if (StringUtils.isNotEmpty(qb.getUserid().getEmail())) {
				sqlString += " and o.Account = '" + qb.getUserid().getEmail() + "' ";
			}
			
			if (StringUtils.isNotEmpty(qb.getNumber())) {
				sqlString += " and o.Number = '" + qb.getNumber() + "' ";
			}
			
			if (qb.getState() != null) {
				sqlString += " and o.State = " + qb.getState() + " ";
			}
			
			
			Session session = getSession();
			result = (List<OrderInformation>) session.createQuery(sqlString).list();
			session.close();
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findOrderInformationBySeqId
	 * @param seqId
	 * @return
	 */
	public OrderInformation findOrderInformationBySeqId(Long seqId) {
		
		OrderInformation result = null;
		try {
			
			if (seqId != null) {
				String sqlString = " select o from OrderInformation o " +
						" inner join fetch o.Userid u  " +
						" inner join fetch o.Paymentmethodseqid p  " + 
						" inner join fetch o.Transportmethodseqid t  " + 
						" where 1=1 ";
				
				sqlString += " and o.Seqid = " + seqId + " ";
				
				Session session = getSession();
				result = (OrderInformation) session.createQuery(sqlString).list().get(0);
				session.close();
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * queryOrderDetailByQb
	 * @param qb
	 * @return
	 */
	public List<OrderDetail> queryOrderDetailByQb(OrderInformation qb, int pageNum, int pageSize) {
		
		List<OrderDetail> result = null;
		try {
			
			String sqlString = " select o from OrderInformation o " +
					" inner join fetch o.Userid u  " +
					" inner join fetch o.Paymentmethodseqid p  " + 
					" inner join fetch o.Transportmethodseqid t  " + 
					" where 1=1 ";
			
			if (StringUtils.isNotEmpty(qb.getNumber())) {
				sqlString += " and o.Number = '" + qb.getNumber() + "' ";
			}
			
			Session session = getSession();
			OrderInformation orderInforObj = (OrderInformation) session.createQuery(sqlString).list().get(0);
			session.close();
			
			if (orderInforObj != null) {
				result = new ArrayList<OrderDetail>(orderInforObj.getOrderDetailSet());
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
	
	/**
	 * findOrderInformationByNumber
	 * @param Number
	 * @return
	 */
	public OrderInformation findOrderInformationByNumber(String Number) {
		
		OrderInformation result = null;
		try {
			
			if (Number != null) {
				String sqlString = " select o from OrderInformation o " +
						" inner join fetch o.Userid u  " +
						" inner join fetch o.Paymentmethodseqid p  " + 
						" inner join fetch o.Transportmethodseqid t  " + 
						" where 1=1 ";
				
				sqlString += " and o.Number = '" + Number + "' ";
				
				Session session = getSession();
				result = (OrderInformation) session.createQuery(sqlString).list().get(0);
				session.close();
			}
			
		} catch(Exception e) {
			logger.debug(e);
		}
		
		return result;
	}
}